package com.practice.draw.customview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View

/**
 * Created by lgs on 2018/7/17.
 */
class DrawColorView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val DENSITY = resources.displayMetrics.density
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)


    override fun onDraw(canvas: Canvas?) {

        canvas?.save()
        canvas?.clipRect(0, 0, width, height / 2)
        canvas?.drawColor(Color.YELLOW)
        canvas?.restore()

        canvas?.save()
        paint.strokeWidth = DENSITY
        paint.textSize = 18 * DENSITY
        paint.textAlign = Paint.Align.CENTER
        canvas?.drawText("颜色绘制作业", width / 2f, height / 2 + DENSITY * 50, paint)

    }

}